import tweepy
import sqlite3
from optparse import OptionParser
import sys
from urllib import urlencode
import re
from numpy import *
from numpy import matrix
from numpy import linalg
import locale
import json
import time
import pdb
import urllib2
import urllib
import httplib

class InterestingContentFollowers:
	def __init__(self, outputfilepath, screen_name):
		self.outputfile = outputfilepath
		self.user_screen_name = screen_name
		self.Process()	

	def Process(self):	
		output = open(self.outputfile, 'w')

		# Authentication tokens for Twitter, authenticate, and check rate limit status
		auth = tweepy.OAuthHandler("Fx66LzrcrCeOveiNyISneg", "12jAy9CJd3XzW2R1xv3IBpdLMi2MC7onrcGKfvUf0Nc")
		auth.set_access_token("309217069-IblbXwYLA4DRcQb8YA6KVgsf3gH3pnerlB6IFmMF", "19bmED10QMLDN1O6AY2xE7kI22MUOhD7wtJPRNvdm8")
		api = tweepy.API(auth)
		my_rate_limit_status = api.rate_limit_status()
		
		# Lists that will be used for users and their items
		items = []
		users = []
		topics = set()
		
		# Get user_screen_name and append it (from now on "me") to the list of users, initialize api calls to 0
		me = api.get_user(self.user_screen_name).id
		my_screen_name = self.user_screen_name
		api_calls = 0
		
		my_friends_ids = api.friends(me)
		api_calls = api_calls + 1
				
		print "Api calls: " + "%s" %api_calls
		if (api_calls > 300):
			print "API Status: " + "%s" %my_rate_limit_status
			print "Sleeping for 61 minutes..."
			time.sleep(60*61)
			api_calls = 0
		
		friend_count = 0
		
		query_args = { 'format' : 'rdf', 'username': "%s" %self.user_screen_name, 'profile type' : 'hashtag'}
	
		print "Arguments for next query: "
		print query_args
		encoded_args = urllib.urlencode(query_args)
		headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}	
		conn = httplib.HTTPConnection("wisserver.st.ewi.tudelft.nl:8080")
		conn.request("POST", "/um-twitter-service/profile/", encoded_args, headers)

		response = conn.getresponse()
		print response.status, response.reason
		data = response.read()
		print "Data length: %s" %len(data)
	
		pattern = re.compile(r'<wi:topic>(\S+\s?\S+)</wi:topic>', re.UNICODE)
		topic_posted = pattern.findall(data)

		for line in topic_posted:
		
			print "line: %s" %line

			pattern = re.compile(r'(https?://\S+)', re.UNICODE)
			nonurl_topic = pattern.findall(line)

			if (len(nonurl_topic) == 0):
				print "New topic found: %s" %line
			
				words_in_tweet = line.split(' ')
				print "Words: %s" %words_in_tweet
				topics.add(words_in_tweet[0])
				print "part0: %s" %words_in_tweet[0]
				try:
					if (len(words_in_tweet[1]) != 0):
						topics.add(words_in_tweet[1])
						print "part1: %s" %words_in_tweet[1]
					if (len(words_in_tweet[2]) != 0):
						topics.add(words_in_tweet[2])
						print "part2: %s" %words_in_tweet[2]
				except:
					continue
				#topics.add(line)
		
		for followee in my_friends_ids:
			
			if not(friend_count > 29):
			
				print "Friends: %s \n" %friend_count
			
				friend_count = friend_count + 1 

				if (users.count(followee.id) == 0):
					users.append(followee.id)
	
				# User profiles can be locked; if so just continue
				try:
					followee_screen_name = api.get_user(followee.id).screen_name
					api_calls = api_calls + 1
				
					print "Api calls: " + "%s" %api_calls
					if (api_calls > 325):
						print "API Status: " + "%s" %my_rate_limit_status
						print "Sleeping for 61 minutes..."
						time.sleep(60*61)
						api_calls = 0
					pass
				except:
					continue
			
				print "Follower %s: " %followee_screen_name	

				# Get Topics from TweetUM	

				query_args = { 'format' : 'rdf', 'username': "%s" %followee_screen_name, 'profile type' : 'hashtag'}
			
				print "Arguments for next query: "
				print query_args
				encoded_args = urllib.urlencode(query_args)
				headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}	
				conn = httplib.HTTPConnection("wisserver.st.ewi.tudelft.nl:8080")
				conn.request("POST", "/um-twitter-service/profile/", encoded_args, headers)

				response = conn.getresponse()
				print response.status, response.reason
				data = response.read()
				print "Data length: %s" %len(data)
			
				pattern = re.compile(r'<wi:topic>(\S+\s?\S+)</wi:topic>', re.UNICODE)
				topic_posted = pattern.findall(data)
	
				for line in topic_posted:
				
					print "line: %s" %line
		
					pattern = re.compile(r'(https?://\S+)', re.UNICODE)
					nonurl_topic = pattern.findall(line)
		
					if (len(nonurl_topic) == 0):
						print "New topic found: %s" %line
					
						words_in_tweet = line.split(' ')
						print "Words: %s" %words_in_tweet
						topics.add(words_in_tweet[0])
						print "part0: %s" %words_in_tweet[0]
						try:
							if (len(words_in_tweet[1]) != 0):
								topics.add(words_in_tweet[1])
								print "part1: %s" %words_in_tweet[1]
							if (len(words_in_tweet[2]) != 0):
								topics.add(words_in_tweet[2])
								print "part2: %s" %words_in_tweet[2]
						except:
							continue
						#topics.add(line)
			
				# Wait 10 seconds so as to not overload the server
				time.sleep(10)			
				conn.close()
				
		print "Here are all the topics: "	
		#for item in topics:
		#	print item
		
		print topics
		
		print "Total number of topics: %s" %len(topics)


##############
def RunMain():
	usage = "(First version) usage: %prog <outputfilepath> <TwitterScreenName>"
	parser = OptionParser(usage)
	(options, args) = parser.parse_args()

	if( len(args) < 1):
		print "Invalid number of arguments. Use InterestingContentFollowers.py --help to see the details."
	else:
		outputfilepath = args[0]
		screen_name = args[1]

		try:
			print "Updating the interesting content followers user-item matrix"

			InterestingContentFollowers(outputfilepath, screen_name)
		except:
			pass
			raise

if( __name__ == "__main__"):
	RunMain()	
